function [newimg] = MaskCorrection(vol,mask,tube)
%% Filter kidney image with a mask and a variable scalar factor
%% applied along the primary axis.
%
%  The bookends, 6 closest to end,  have a large (linearly decreasing) factor
%  applied to the mask.	 From the bookends to the centre of the main
%  axis, a gentler scaling factor is applied to the mask.
%
%   - Michael Eager,  (michael.eager@monash.edu)
%   - (c) 2012, Monash Biomedical Imaging,
%   Monash University, Australia

%     Copyright © 2012-2013 Michael Eager <michael.eager@monash.edu> 
%
%     This file is part of Xglom.
% 
%     This is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/>.

%corr = inhomogenityCorrection(vol,sigma);
if nargin ~=3
    tube = ones(size(vol));
end


len = size(vol);
steps = (len(1)/2) - 6;
zfactor = [12:-2:2 1.5:-(0.5/steps):1.0 1.0:(0.5/steps):1.5 2:2:12];
for ii = 1:len(1)
        newimg(:,:,ii) = squeeze(tube(ii,:,:)).*squeeze(vol(ii,:,:))./( mask * zfactor(ii) + 1);
end
newimg = permute(newimg,[3 1 2]);
newimg= NormaliseImage(newimg);
